<style type="text/css">

#puma {
  border-collapse: collapse;
}

#puma td {
  border: 1px solid silver;
  text-align: center;
}

</style>
<form action="/backend_dev.php/matrix/save" method="post">
<table id="puma">
  <input type="hidden" name="groups" value="<?php echo $groups[1] ?>_<?php echo $groups[2] ?>" />
  <?php for($i = 0; $i <= count($primary); $i++): ?>
    <tr>
      <?php if(!$i): ?>
        <td></td>
      <?php else: ?>
        <td><?php echo $primary[$i-1]['title'] ?></td>
      <?php endif ?>
      <?php for($j = 0; $j < count($secondary); $j++): ?>
        <?php if(!$i): ?>
          <td><?php echo $secondary[$j]['title'] ?></td>
        <?php else: ?>
          <td><input type="hidden" name="extern[<?php echo $primary[$i-1]['id'] ?>][<?php echo $secondary[$j]['id'] ?>]" value="0" /><input type="checkbox" name="extern[<?php echo $primary[$i-1]['id'] ?>][<?php echo $secondary[$j]['id'] ?>]" value="1" /></td>
        <?php endif ?>
      <?php endfor ?>
    </tr>
  <?php endfor; ?>
</table>
<div style="margin-top: 10px; text-align: right;"><input type="submit" value="Save" /></div>
</form>